package com.wkx.controller;/**
 * @className: AdminController
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/7
 **/

import com.wkx.pojo.Admin;
import com.wkx.pojo.Roles;
import com.wkx.service.AdminService;
import com.wkx.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : AdminController  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/7  19:32
 */
@Controller
@RequestMapping("admin")
public class AdminController {
    private Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Reference(url = "dubbo://localhost:20881")
    private AdminService adminService;
    @Reference(url = "dubbo://localhost:20881")
    private RoleService roleService;

    @RequestMapping("login")
    public String login(Admin admin, HttpSession session){
        logger.info("登陆方法");
        String name = SecurityContextHolder.getContext().getAuthentication().getName();

        Admin admin1 = adminService.login(name);

        session.setAttribute("user",admin1);
        return "redirect:/main/toMain";


    }
    @RequestMapping("toAdmin")
    public String toAdmin(Model model,@RequestParam(value = "page",defaultValue = "1") Integer page){
        logger.info("跳转至用户角色管理页面");
        List<Admin> adminList = adminService.getAdminList(page);
        Map<Integer,Object> map = new HashMap<>();
        for (Admin admin : adminList) {
            List<Roles> rolesListByAid = roleService.getRolesListByAid(admin.getaId());
            map.put(admin.getaId(),rolesListByAid);
        }
        model.addAttribute("adminList",adminList);
        model.addAttribute("map",map);
        return "admin";
    }

    @RequestMapping("vokeRole")
    public String voke(Integer aid,Model model){
        logger.info("跳转至角色授权页面");
        List<Roles> adminRolesList = roleService.getRolesListByAid(aid);
        Admin admin = adminService.getAdminById(aid);
        StringBuilder s = new StringBuilder();
        for (Roles roles : adminRolesList) {
            s.append(roles.getId());
        }
        List<Roles> rolesList = roleService.getRolesList();
        model.addAttribute("s", s.toString());
        model.addAttribute("admin", admin);
        model.addAttribute("rolesList",rolesList);
        return "vokeRole";
    }

    @RequestMapping("toVoke")
    public String toVoke(Integer[] roles,Integer aid){
        logger.info("角色授权后跳转至用户角色管理页面");
        if (roles==null) {
            roles = new Integer[0];
        }
        roleService.updateRoles(roles, aid);
        return "redirect:/admin/toAdmin";
    }
}
